/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2007 Neil J. Patel <njpatel@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * Authors: Neil J. Patel <njpatel@gmail.com>
 *
 */

#include "applet-button.h"

#include <panel-applet.h>

#include "affinity.h"
#include "aff-settings.h"
#include "aff-window.h"
#include "aff-utils.h"

typedef struct {
	PanelApplet *applet;
	
	AffinityApp *app;
	AffSettings *settings;
	
	GtkWidget *button;
} AffinityMenu;


static GtkWidget* menu_button_new(AffinityMenu *menu);
static gboolean menu_button_pressed (GtkWidget *widget, GdkEventButton *event,
					  AffinityMenu *menu);
static void applet_button_toggle_false (AffinityApp *app, AffinityMenu *menu);
static void applet_button_toggle_true (AffinityApp *app, AffinityMenu *menu);
static void applet_button_toggled (GtkToggleButton *togglebutton, AffinityMenu *menu);
static gboolean applet_button_pressed (GtkWidget *widget, GdkEventButton *event, AffinityMenu *menu);

GtkWidget*
applet_button_new(PanelApplet *applet)
{
        AffinityMenu *menu;
        AffinityApp *app;
        menu = g_new0(AffinityMenu, 1);
        menu->applet = applet;
      
	app = affinity_app_new (TRUE);
	menu->app = app;
	
	menu->settings = app->settings;
	
	g_signal_connect (G_OBJECT (menu->app), "affinity-shown",
			  G_CALLBACK (applet_button_toggle_true), (gpointer)menu);        	

	g_signal_connect (G_OBJECT (menu->app), "affinity-hidden",
			  G_CALLBACK (applet_button_toggle_false), (gpointer)menu);
	
        menu->button = menu_button_new(menu);	
	return menu->button;
}


static GtkWidget*
menu_button_new(AffinityMenu *menu)
{
        GtkWidget *button;
        GtkWidget *image;
        GdkPixbuf *pixbuf = NULL;
        
        button = gtk_toggle_button_new_with_label(menu->settings->applet_name);

	pixbuf = aff_utils_get_app_icon_sized (menu->settings->applet_icon, 22, 22);
        image = gtk_image_new_from_pixbuf(pixbuf);
        
        gtk_button_set_image(GTK_BUTTON(button), image);
        gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE);
        
        g_signal_connect (G_OBJECT (button),
			  "button-press-event",
			  G_CALLBACK (applet_button_pressed),(gpointer) menu);
	
	g_signal_connect (G_OBJECT (button),
			  "toggled",
			  G_CALLBACK (applet_button_toggled),(gpointer) menu);		  			  
        
        return button; 
}

static void 
applet_button_toggle_false (AffinityApp *app, AffinityMenu *menu)
{
	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (menu->button), FALSE);
}

static void 
applet_button_toggle_true (AffinityApp *app, AffinityMenu *menu)
{
	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (menu->button), TRUE);
}

static gboolean
applet_button_pressed (GtkWidget *widget, GdkEventButton *event, AffinityMenu *menu)
{
	gboolean active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget));
	g_signal_stop_emission_by_name ((gpointer)widget, "button-press-event");
	
	switch (event->button) {
		case 1:
			gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), !active);
			return TRUE;
			break;
		default:
			break;
	}
	return FALSE;
}

/* shamelessly taken from SUSEs SLAB menu (gnome-main-menu)*/
static void 
applet_button_toggled (GtkToggleButton *togglebutton, AffinityMenu *menu)
{
	gboolean active = gtk_toggle_button_get_active (togglebutton);
	AffinityApp *app = AFFINITY_APP (menu->app);
		
	GdkRectangle button_geom;
	GdkRectangle slab_geom;
	GdkRectangle monitor_geom;

	PanelAppletOrient orient;
	GdkScreen *panel_button_screen;

	gdk_window_get_origin (GTK_WIDGET (togglebutton)->window, & button_geom.x, & button_geom.y);
	button_geom.width  = GTK_WIDGET (togglebutton)->allocation.width;
	button_geom.height = GTK_WIDGET (togglebutton)->allocation.height-4;

	slab_geom.width  = app->window->allocation.width;
	slab_geom.height = app->window->allocation.height;

	panel_button_screen = gtk_widget_get_screen (GTK_WIDGET (togglebutton));

	gdk_screen_get_monitor_geometry (
		panel_button_screen,
		gdk_screen_get_monitor_at_window (
			panel_button_screen, GTK_WIDGET (menu->button)->window),
		& monitor_geom);

	orient = panel_applet_get_orient (menu->applet);

	switch (orient) {
		case PANEL_APPLET_ORIENT_UP:
			slab_geom.x = button_geom.x;
			slab_geom.y = button_geom.y - slab_geom.height;
			break;

		case PANEL_APPLET_ORIENT_DOWN:
			slab_geom.x = button_geom.x;
			slab_geom.y = button_geom.y + button_geom.height;
			break;

		case PANEL_APPLET_ORIENT_RIGHT:
			slab_geom.x = button_geom.x + button_geom.width;
			slab_geom.y = button_geom.y;
			break;

		case PANEL_APPLET_ORIENT_LEFT:
			slab_geom.x = button_geom.x - slab_geom.width;
			slab_geom.y = button_geom.y;
			break;

		default:
			slab_geom.x = 0;
			slab_geom.y = 0;
			break;
	}

	if (orient == PANEL_APPLET_ORIENT_UP || orient == PANEL_APPLET_ORIENT_DOWN) {
		if ((slab_geom.x + slab_geom.width) > (monitor_geom.x + monitor_geom.width))
			slab_geom.x = button_geom.x + button_geom.width - slab_geom.width;
	}
	else {
		if ((slab_geom.y + slab_geom.height) > (monitor_geom.y + monitor_geom.height))
			slab_geom.y = button_geom.y + button_geom.height - slab_geom.height;
	}

	gtk_window_move (GTK_WINDOW (app->window), slab_geom.x, slab_geom.y);
	
	if (active)
		affinity_app_show (app);		
	else
		affinity_app_hide (app);
	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (menu->button), active);
}

